var url;
var arr = [];
var arr1 = [];
var page=0;
Page({
      data: {
            title: [],
            list: []
      },
      onLoad: function(options) {
            wx.showLoading({
                  title: '正在加载',
            })
            wx.setNavigationBarTitle({
                  title: options.title,
            })
            var that = this;   
            console.log(options);
            if (options.title == "正在上映的电影-北京") {
                  url = 'http://localhost:8080/v2/movie/in_theaters?count=20'
            }
            if (options.title == "豆瓣电影Top250") {
                  url = 'http://localhost:8080/v2/movie/top250?count=20'
            }
            if (options.title == "即将上映的电影") {
                  url = 'http://localhost:8080/v2/movie/coming_soon?count=20'
            }
            wx.request({
                  url: url, //仅为示例，并非真实的接口地址
                  success(res) {
                        console.log(res.data)
                        //数据加载成功;取消loading
                        wx.hideLoading();
                        res.data.subjects.forEach(function(item) {
                              const arr2 = [];
                              var num = parseInt(Number(item.rating.stars) / 10);
                              for (var i = 0; i < 5; i++) {
                                    if (i < num) {
                                          arr2.push(1)
                                    } else {
                                          arr2.push(0)
                                    }
                              }
                              item.newstar = arr2;
                        })
                        arr.unshift(res.data.title);
                        arr1.unshift(res.data.subjects);
                        that.setData({
                              title: arr,
                              list: arr1
                        })
                  }
            })

      },
      onPullDownRefresh: function() {
            console.log('下拉刷新')
            page=0;
            this.onReachBottom();
      },
      onReachBottom: function() {
            var that=this;
            page++;
            wx.showLoading({
                  title: '拼命加载中',
            })
            wx.request({
                  url: url+'&start='+(page*20), //仅为示例，并非真实的接口地址
                  success(res) {
                        console.log(res.data)
                        //数据加载成功;取消loading
                        wx.hideLoading();
                        res.data.subjects.forEach(function (item) {
                              arr1[0].push(item);
                              const arr2 = [];
                              var num = parseInt(Number(item.rating.stars) / 10);
                              for (var i = 0; i < 5; i++) {
                                    if (i < num) {
                                          arr2.push(1)
                                    } else {
                                          arr2.push(0)
                                    }
                              }
                              item.newstar = arr2;
                            
                        })
                        var current = res.data.subjects;
                        that.setData({
                              title: arr,
                              list: arr1.concat(current)
                        })
                  }
            })
      },
      toDetail: function(event) {
            const id = event.currentTarget.dataset.id;
            wx.navigateTo({
                  url: '/pages/detail/detail?id=' + id,
            })
      },
})